﻿using System;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;


namespace TcpServer.DataProcessor
{
    class Alarm2011Processor 
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Alarm2011Processor));

        public static void Persist(DustContext context,string msg)
        {
            var cn = Parser.GetStringByKey("CN",msg);
            if (cn == null || cn.Length != 4) return;
            // var alarmTime = Parser.GetDateTimeByKeyFromContent("DataTime", content.ReceivedMsg);
            var thresholdType = BaseInfoBufferAndHelper.GetThresholdType(cn);
            var mn= Parser.GetStringByKey("MN", msg);
            if (string.IsNullOrWhiteSpace(mn)) return;
            try
            {
                //有效字符串
                if (Parser.IsLengthRight(msg))
                {
                    //根据MN号获取基本信息
                    var time = Parser.GetDateTimeByKey("DataTime", msg);
                    //从字符串获取污染物代码s
                    var pollutantCodes = Parser.GetPollutantCodes(msg);
                    if (time.HasValue)
                    {
                        var section = BaseInfoBufferAndHelper.GetAlarmThrsholdTimeType(time.Value);
                        foreach (var code in pollutantCodes)
                        {
                            var equipment = BaseInfoBufferAndHelper.GetBaseWholeInfoByMn(mn, code, section);
                            //有效企业信息
                            if (equipment != null && equipment.SiteTypeID != null)
                            {
                                //var factor = BaseInfoBuffer.GetVFactorInfo(code, equipment.SiteTypeID.Value);
                                var flag = Parser.GetStringByKey(code + "-Flag", msg);
                                if (!flag.Equals("N"))
                                {
                                    break;
                                }
                                var rtd = Parser.GetDoubleByKey(code + "-Rtd", msg);
                                if (!rtd.HasValue || equipment.报警阈值 >= rtd.Value) continue;
                                var alarm = new 报警记录()
                                {
                                    监测点编号StatCode = equipment.StatCodeID,
                                    设备编号DevCodeID = equipment.DevCodeID,
                                    时间 = DateTime.Now,
                                    内容 = "超标:" + equipment.因子名称 + "值为" + rtd + "时间为" + time + "(限值" + equipment.报警阈值 + ")",
                                    是否发送 = false,
                                    短信接收人 = "",
                                    报警限值 = equipment.报警阈值,
                                    报警限值时间类型ID = equipment.报警限值时间类型ID,
                                    报警限值类型ID = thresholdType?.ID,
                                    工地类型ID = equipment.工地类型ID,
                                    次数 = 1,
                                    监测因子ID = equipment.MonitorFactorID

                                };
                                context.报警记录.Add(alarm);
                                context.SaveChanges();
                            }
                            else
                            {
                               log.Warn( mn + " 没有对应基础信息！");
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error( ex);
                log.Error( msg);

            }
        }
       
    }
}
